﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using Engine.Dtd.Schema;
using Engine.Dtd;
using Engine.Dtd.Configuration;
using System.Configuration;

namespace Engine.Sgml.Test {
    /// <summary>
    /// Summary description for DtdSchemaTester
    /// </summary>
    [TestClass]
    public class DtdSchemeTest {
        public DtdSchemeTest() {
            //
            // TODO: Add constructor logic here
            //
            }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
                }
            set {
                testContextInstance = value;
                }
            }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() {
            DirectoryInfo fileDirectory = Directory.GetParent(TestContext.TestDir).Parent;

            DirectoryInfo schemaDirectory = new DirectoryInfo(Path.Combine(fileDirectory.FullName, @"Engine.Sgml.Test\Scheme Samples"));
            DirectoryInfo saveDirectory = new DirectoryInfo(Path.Combine(fileDirectory.FullName, @"Engine.Sgml.Test\Saved Docs"));
            fileDirectory = new DirectoryInfo(Path.Combine(fileDirectory.FullName, @"Engine.Sgml.Test\Document Samples"));

            Assert.IsTrue(schemaDirectory.Exists);
            Assert.IsTrue(fileDirectory.Exists);
            Assert.IsTrue(saveDirectory.Exists);

            schemaDir = schemaDirectory.FullName;
            }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private string schemaDir;


        private Uri GetUrl(string fileName) {
            return new Uri(Path.Combine(schemaDir, fileName));
            }

        [TestMethod]
        public void CreateEntityCatalogTest() {
            EntityCatalogSection section = new EntityCatalogSection();
            // DTD Schemas
            // HTML
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD HTML 4.0//EN", "strict.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD HTML 4.0 Transitional//EN", "loose.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD HTML 4.01 Transitional//EN", "loose.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));
            section.PublicEntities.Add(new PublicElement("-//W3C//DTD XHTML 1.0 Frameset//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"));
            // Mobile
            section.PublicEntities.Add(new PublicElement("-//WAPFORUM//DTD XHTML Mobile 1.0//EN", "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"));
            // ISO files
            section.PublicEntities.Add(new PublicElement("ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML", "http://www.w3.org/2003/entities/iso8879/isolat1.ent"));
            
            // Add and save the section in a configuration file
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.Sections.Remove("catalog");
            config.Sections.Add("catalog", section);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("catalog");
            }

        [TestMethod]
        public void GetDElementDeclarationTest() {
            DtdSchema schema = DtdSchema.Load(GetUrl("Xhtml1-strict.dtd"), "html");
            DElementDeclaration u = schema.GetElementDeclaration("u");
            Assert.IsNull(u);
            u = schema.GetElementDeclaration("u");
            Assert.IsNull(u);
            DElementDeclaration U = schema.GetElementDeclaration("U");
            Assert.IsNull(U);
            U = schema.GetElementDeclaration("U");
            Assert.IsNull(U);

            schema = DtdSchema.Load(new Uri("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"), "html");
            u = schema.GetElementDeclaration("u");
            Assert.IsNull(u);
            U = schema.GetElementDeclaration("U");
            Assert.IsNull(U);
            }

        [TestMethod]
        public void LoadReportSchema() {
            DtdSchema schema = DtdSchema.Load(GetUrl("Report.dtd"), "REPORT");
            }

        [TestMethod]
        public void LoadCatalogSchema() {
            DtdSchema schema = DtdSchema.Load(GetUrl("Catalog.dtd"), "CATALOG");
            }

        [TestMethod]
        public void LoadTvScheduler() {
            DtdSchema schema = DtdSchema.Load(GetUrl("TV Scheduler.dtd"), "TVSCHEDULE");
            }

        [TestMethod]
        public void LoadNewsPaper() {
            DtdSchema schema = DtdSchema.Load(GetUrl("News Paper.dtd"), "NEWSPAPER");
            }

        [TestMethod]
        public void LoadStrictSchema() {
            DtdSchema schema = null;
            try {
                schema = DtdSchema.Load(new Uri("http://www.w3.org/TR/REC-html40/strict.dtd"), "html");
                }
            catch (DtdException) {
                }
            schema = DtdSchema.Load(new Uri("http://www.w3.org/TR/REC-html40/strict.dtd"), "HTML");
            }

        [TestMethod]
        public void LoadXhtmlStrictSchema() {
            UrlResolver resolver = new UrlResolver();
            HttpRequestSettings settings = new HttpRequestSettings();
            settings.Timeout = 200000;
            Uri address = new Uri("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
            string contentType;
            SgmlStream stream = resolver.GetEntity(address, settings, out contentType);
            DtdSchema schema = DtdSchema.Load(stream, address, "html");
            }

        [TestMethod]
        public void LoadFramesetSchema() {
            UrlResolver resolver = new UrlResolver();
            HttpRequestSettings settings = new HttpRequestSettings();
            settings.Timeout = 200000;
            Uri address = new Uri("http://www.w3.org/TR/REC-html40/frameset.dtd");
            string contentType;
            SgmlStream stream = resolver.GetEntity(address, settings, out contentType);
            DtdSchema schema = DtdSchema.Load(stream, address, "HTML");
            }

        [TestMethod]
        public void LoadLooseSchema() {
            UrlResolver resolver = new UrlResolver();
            HttpRequestSettings settings = new HttpRequestSettings();
            settings.Timeout = 200000;
            Uri address = new Uri("http://www.w3.org/TR/REC-html40-971218/loose.dtd");
            string contentType;
            SgmlStream stream = resolver.GetEntity(address, settings, out contentType);
            DtdSchema schema = DtdSchema.Load(stream, address, "HTML");
            }

        [TestMethod]
        public void LoadHTMLSpecFromInternetSchema() {
            UrlResolver resolver = new UrlResolver();
            HttpRequestSettings settings = new HttpRequestSettings();
            settings.Timeout = 200000;
            Uri address = new Uri("http://www.w3.org/MarkUp/html-spec/html.dtd");
            string contentType;
            SgmlStream stream = resolver.GetEntity(address, settings, out contentType);
            DtdSchema schema = DtdSchema.Load(stream, address, "HTML");
            }

        public void LoadHTMLSpecSchema() {
            DtdSchema schema = DtdSchema.Load(GetUrl("ietf dtd html en.dtd"), "HTML");
            }

        [TestMethod]
        public void LoadAnthologySchema() {
            DtdSchema schema = DtdSchema.Load(GetUrl("Anthology.dtd"), "ANTHOLOGY");
            }

        [TestMethod]
        public void LoadResumeSchema() {
            DtdSchema schema = DtdSchema.Load(GetUrl("Resume.dtd"), "resume");
            }

        [TestMethod]
        public void LoadDefaultSchema() {
            DtdSchema schema = SgmlDocument.LoadSchemaFromManifest();
            }

        [TestMethod]
        public void LoadEmptySchema() {
            DtdSchema schema = null;
            try {
                schema = DtdSchema.Load(GetUrl("Empty.dtd"), "empty");
                }
            catch (DtdException e) {
                string message = e.Message;
                string context = e.Context;
                }
            }
        }
    }
